//
//  SINAIdentitySelectViewController.m
//  sinashebao
//
//  Created by  androidlongs on 2017/12/19.
//  Copyright © 2017年  androidlongs. All rights reserved.
// 身份选择页面

#import "SINAIdentitySelectViewController.h"
#import "SINAStudentTabBarViewController.h"
#import "SINATeacherTabBarViewController.h"
#import "SINAIdentitySelectViewController.h"
#import "SINAStudentMainViewController.h"
#import "SINAUserModel.h"
#import "SINATeacherMainViewController.h"
#import "SINACommonUserHelper.h"
#import "SINAHttpRequestUtils.h"
#import "SINAUiviewClickLiserner.h"
#import "SINACommonUserHelper.h"
#import "SINATecApproveUserNameViewController.h"
#import "SINAStuApproveUserNameViewController.h"
#import "SINAControllerUtils.h"
#import "JPUSHService.h"

@interface SINAIdentitySelectViewController ()


@property (weak, nonatomic) IBOutlet UIButton *nextButton;
@property(nonatomic,strong) NSString *identitys;
@property (weak, nonatomic) IBOutlet UIImageView *reacherImageView;
@property (weak, nonatomic) IBOutlet UIImageView *studentImageView;


//老师
@property (weak, nonatomic) IBOutlet UIView *selectTecherView;
//老师图片
@property (weak, nonatomic) IBOutlet UIImageView *selectTecherImageView;


//学生
@property (weak, nonatomic) IBOutlet UIView *selectStudentView;
//学生图片
@property (weak, nonatomic) IBOutlet UIImageView *selectStudentImageView;

@property(assign,nonatomic) BOOL isTest;
@end

@implementation SINAIdentitySelectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    if (UI_IS_IPHONE6PLUS) {
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"nav_bar_plus_bg"]  forBarMetrics:UIBarMetricsDefault];
    } else {
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"nav_bar_bg"]  forBarMetrics:UIBarMetricsDefault];
    }
    
    // Do any additional setup after loading the view from its nib.
    self.isTest=NO;
    //设置lable的圆角显示
    self.nextButton.layer.cornerRadius =5;
    //设置lable的边框 宽度为1
    self.nextButton.layer.borderWidth=0;
    //设置lable的边框颜色
    self.nextButton.layer.borderColor=[UIColor redColor].CGColor;
    //设置 显示大小范围为 border内容范围中
    self.nextButton.clipsToBounds = YES;
    self.identitys=@"2";
    [self.nextButton setBackgroundColor: baseButtonBackGroundColor];
    
    WeakSelf;
    [[SINAUiviewClickLiserner alloc]viewClickFunction:self.selectTecherView andWithBlick:^(UIView *view) {
        weakSelf.identitys=@"2";
        weakSelf.reacherImageView.image=[UIImage imageNamed:@"setting_select_icon"];
        weakSelf.studentImageView.image=[UIImage imageNamed:@"setting_select_no_icon"];
        weakSelf.selectTecherImageView.image=[UIImage imageNamed:@"老师选中"];
        weakSelf.selectStudentImageView.image=[UIImage imageNamed:@"学生"];
    }];
    
    [[SINAUiviewClickLiserner alloc]viewClickFunction:self.selectStudentView andWithBlick:^(UIView *view) {
        
        weakSelf.identitys=@"1";
        weakSelf.studentImageView.image=[UIImage imageNamed:@"setting_select_icon"];
        weakSelf.reacherImageView.image=[UIImage imageNamed:@"setting_select_no_icon"];
        weakSelf.selectTecherImageView.image=[UIImage imageNamed:@"老师"];
        weakSelf.selectStudentImageView.image=[UIImage imageNamed:@"学生选中"];
    }];
}

-(void)viewWillAppear:(BOOL)animated{
    
    self.navigationController.navigationBarHidden = YES;
    self.navigationController.navigationBar.barTintColor = baseSelectColor;
    UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
    if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
        statusBar.backgroundColor = baseSelectColor;
    }
    
}
-(void)viewDidAppear:(BOOL)animated{
    UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
    if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
        statusBar.backgroundColor = [UIColor clearColor];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//选择身份为学生
//用户身份标识  1学生  2教师 -1没有选择身份
- (IBAction)selectStudentClickFunction:(UIButton *)sender {
    self.identitys=@"1";
    self.studentImageView.image=[UIImage imageNamed:@"setting_select_icon"];
    self.reacherImageView.image=[UIImage imageNamed:@"setting_select_no_icon"];
    self.selectTecherImageView.image=[UIImage imageNamed:@"老师"];
    self.selectStudentImageView.image=[UIImage imageNamed:@"学生选中"];
}

//选择身份为老师
- (IBAction)selectTeacherClickFunction:(UIButton *)sender {
    self.identitys=@"2";
    self.reacherImageView.image=[UIImage imageNamed:@"setting_select_icon"];
    self.studentImageView.image=[UIImage imageNamed:@"setting_select_no_icon"];
    self.selectTecherImageView.image=[UIImage imageNamed:@"老师选中"];
    self.selectStudentImageView.image=[UIImage imageNamed:@"学生"];
}
-(void) commitSelectInfromaion:(NSString*) ident {
    
    if (self.isTest) {
        [self toHomePageFunction];
        return;
    }
    
    
    SINACommonUserHelper *helper =[SINACommonUserHelper share];
    SINAUserModel *userModel = [helper getUserModel];
    if (userModel!=nil&&userModel.userUid!=nil&&![userModel.userUid isEqualToString:@""]) {

        [self startLoading];
        NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
        [dict setObject:userModel.userUid forKey:@"uuid"];
        [dict setObject:self.identitys forKey:@"identity"];
        NSMutableDictionary *dict2 = [[NSMutableDictionary alloc]init];
        [dict2 setObject:[SINAHttpRequestUtils encodeDicParams:dict] forKey:@"sj"];
        WeakSelf;
        [CXAFNHttpRequestUtils httpPostRequest:[NSString stringWithFormat:@"%@%@",baseUrl,userAlertInformationUrl] withParamters:dict2 success:^(id responseData) {
            NSString *code = responseData[@"code"];
            [weakSelf stopLoading];
            if ([code isEqualToString:@"1000"]) {
                [weakSelf toHomePageFunction];
            }else{
                NSString *message =responseData[@"message"];
                WISAlertView *alertView =  [[WISAlertView alloc]initWithTitle:@"提示" message:message sureBtn:@"确定" cancleBtn:nil];
                [alertView showXLAlertView];
            }
            
        } failure:^(NSError *error, NSInteger errCode) {
            DLog(@"加载 失败  %@ ",error.debugDescription);
            [weakSelf stopLoading];
            WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"登录验证失败" sureBtn:@"确定" cancleBtn:nil];
            [alertView showXLAlertView];
        }];
    }else{
        
        WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"用户信息异常请重新进入" sureBtn:@"确定"
                                                             cancleBtn:nil];
        [alertView showXLAlertView];
    }
}
/**
 *跳转首页面
 */
-(void) toHomePageFunction{
    SINACommonUserHelper *helper =[SINACommonUserHelper share];
    SINAUserModel *userModel = [helper getUserModel];
    userModel.identitys=self.identitys;
    [helper saveUserModel:userModel];
    WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"您是否进行实名认证" sureBtn:@"确定"
                                                         cancleBtn:@"取消"];
    WeakSelf;
    alertView.resultIndex = ^(NSInteger index) {
        //去认证
        if ([weakSelf.identitys isEqualToString:@"1"]) {
            SINAStuApproveUserNameViewController *stuApproveUserNameController = [[SINAStuApproveUserNameViewController alloc]initWithNibName:@"SINAStuApproveUserNameViewController" bundle:nil];
            [weakSelf.navigationController pushViewController:stuApproveUserNameController animated:YES];
        }else{
//            SINATecApproveUserNameViewController *tecApproveUserNameController = [[SINATecApproveUserNameViewController alloc]initWithNibName:@"SINATecApproveUserNameViewController" bundle:nil];
//            [weakSelf.navigationController pushViewController:tecApproveUserNameController animated:YES];
            SINAStuApproveUserNameViewController *stuApproveUserNameController = [[SINAStuApproveUserNameViewController alloc]initWithNibName:@"SINAStuApproveUserNameViewController" bundle:nil];
            [weakSelf.navigationController pushViewController:stuApproveUserNameController animated:YES];
        }
    };
    alertView.cancleIndex = ^(NSInteger index) {
        //取消认证
        ////用户身份标识  1学生  2教师 -1没有选择身份
        SINACommonUserHelper *helper =[SINACommonUserHelper share];
        SINAUserModel *userModel = [helper getUserModel];
        userModel.identitys=weakSelf.identitys;
        //更新用户数据
        [helper saveUserModel:userModel];
        //home页面
        if ([weakSelf.identitys isEqualToString:@"1"]) {
            SINAStudentTabBarViewController *tabBarViewController = [[SINAStudentTabBarViewController alloc]init];
            [[SINAControllerUtils alloc]restoreRootViewController:tabBarViewController andWithWindow:weakSelf.view.window];;
        } else {
            SINATeacherTabBarViewController *tabBarViewController = [[SINATeacherTabBarViewController alloc]init];
            [[SINAControllerUtils alloc]restoreRootViewController:tabBarViewController andWithWindow:weakSelf.view.window];;
        }
    };
    [alertView showXLAlertView];
    
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */
//下一步
- (IBAction)nextClickFunction:(id)sender {
    [self commitSelectInfromaion:self.identitys];
}
//退出登录
- (IBAction)exitLoginClickFucnion:(id)sender {
    //移除推送
    [JPUSHService setAlias:@"" completion:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
    } seq:101];
    //清除用户信息
    [[SINACommonUserHelper share] clearUserModel];
    [self.navigationController popToRootViewControllerAnimated:YES];
    //    SINALoginViewController *rootViewControll = [[SINALoginViewController alloc]init ];
    //    SINANavLoginViewController *homeController = [[SINANavLoginViewController alloc] initWithRootViewController:rootViewControll];
    //    [[SINAControllerUtils alloc]restoreRootViewController:homeController andWithWindow:self.view.window];;
}


@end
